import java.awt.Color;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
 * 
 * Create Book Report GUI and Generate Book statistic and Book popularity Reports.
 *
 */

public class BookReportControl {
	/**
	 * beg - Book Report GUI.Receive and display data.
	 */
	public BookReportGUI beg;
	/**
	 * searchp - data received from the user and will be used to generate  Book popularity Report
	 
	
	 
	
	
	 * float1,result_temp,temp - temporary Sting[]
	 */
	public String[] searchp;
	/**
	 * searchs - data received from the user and will be used to generate  Book statistic Report
	 */
	public String[] searchs;
	/**
	  * resultp - data received from the DataBase for Book popularity Report and will be Displayed to the user. 
	 */
	public String[] resultp;
	/**
	 * results - data received from the DataBase for Book statistic Report and will be Displayed to the user. 
	 */
	public String[] results;
	/**
	 * float1 - temporary Sting[] 
	 */
	public String[] float1;
	/**
	 * bar_name - Save the name of the bars in the histogram.
	 */
	public String[] bar_name;
	/**
	 * result_temp - temporary Sting[]
	 */
	public String[] result_temp;
	/**
	 *  * update- - data received from the SearchBook to update number of searches.
	 */
	public String[] update;
	/**
	  *temp - temporary Sting[]
	 */
	public String[] temp;
	/**
	 * tmp - temporary Sting
	 */
	public String tmp;
	/**
	 * mone,mechane - for calculate Book popularity
	 */
	float mone;
	/**
	 * mechane - for calculate Book popularity 
	 */
	float mechane;
	int i;
	/**
	 * bar_size,bar_size1 - Save the height of the two bars in the histogram
	 */
	int [] bar_size;
	/**
	 * bar_size1 - Save the height of the two bars in the histogram 
	 */
	int [] bar_size1;
	/**
	 * histo - Create and draw the histogram
	 */
	HistogramFrame histo;
	/**
	 * Create beg(BookReportGUI).
	 * @param visable - set visibility of beg.
	 */
	public BookReportControl(Boolean visable){
		beg=new BookReportGUI(this);
		beg.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		beg.setSize( 350,300 ); // set frame size
		beg.setVisible( visable ); // display frame
	}
	/**
	 * Request for data from Database,calculate and display Book popularity 
	 * @param st - Data about the book from user.
	 */
	public void lSearchBookp(String st)
	{
		
		result_temp=st.split("\\:");
		searchp=new String[3];
		searchp[0]=Main.gusers.id;
		searchp[1]="searchbookp";
		searchp[2]=result_temp[0];
		Main.log.connection.accept(searchp[1]);
		while(Main.log.flag==false);
		Main.log.flag=false;
		tmp=ChatClient.getRs();
		resultp=tmp.split("\\%");
		if(resultp[0].equals(""))
		{
			JOptionPane.showMessageDialog(null,"There is no user with this UserName","ERROR",JOptionPane.ERROR_MESSAGE);
			return;
		}
		else
			//lsearchUpdate(st);
			float1=resultp[0].split("\\*");
			mone=Float.parseFloat(float1[0])*100;
			mechane=Float.parseFloat(float1[1]);
			Main.log.frame.setVisible(false);
			Main.log.frame.setEnabled(false);
			beg.jContentPane.setVisible(true);
			beg.setEnabled(true);
			beg.setTextp(result_temp[0], mone/mechane);
	
	}
	/**
	 * Request data from the database to Generate Book statistic Report and Create it.
	 * @param st - Data about the book from user.
	 * @param in - Start date  to Generate Book statistic Report
	 * @param out - End date  to Generate Book statistic Report
	 */
	public void lSearchBooks(String st,int[] in,int[] out)
	{
		searchs=new String[3];
		searchs[0]=Main.gusers.id;
		searchs[1]="searchbooks";
		searchs[2]=st+"*"+in[0]+"/"+in[1]+"/"+in[2]+"*"+out[0]+"/"+out[1]+"/"+out[2];
		Main.log.connection.accept(searchs[1]);
		while(Main.log.flag==false);
		Main.log.flag=false;
		tmp=ChatClient.getRs();
		results=tmp.split("\\%");
		if(results[0].equals(""))
		{
			JOptionPane.showMessageDialog(null,"There is no data for this book in these dates","ERROR",JOptionPane.ERROR_MESSAGE);
			return;
		}
		else
		{
			String[] name_temp,tempo;
			String tmp12;
			int j1;
			bar_name=results[0].split("\\*");
			name_temp=new String[bar_name.length];
			for(i=0;i<bar_name.length;i++)
				name_temp[i]="";
			for(i=0;i<bar_name.length;i++)
			{
				tempo=bar_name[i].split("\\-");
				tmp12=tempo[1]+"/" +tempo[0];
				for(j1=0;j1<bar_name.length;j1++)
				{
					if(name_temp[j1].equals(tmp12))
						j1=bar_name.length+100;
				}
				if(j1!=bar_name.length+101)
					for(j1=0;j1<bar_name.length;j1++)
						if(name_temp[j1].equals(""))
						{
							name_temp[j1]=tmp12;
							j1=bar_name.length+1;
						}
			}
			j1=0;
			for(i=0;i<bar_name.length;i++)
				if(!name_temp[j1].equals(""))
					j1++;
			bar_name=new String[j1];
			for(i=0;i<bar_name.length;i++)
				bar_name[i]=name_temp[i];
			result_temp=results[1].split("\\$");
			bar_size=new int[bar_name.length];
			bar_size1=new int[bar_name.length];
			for(i=0;i<bar_size.length;i++)
			{
				bar_size[i]=0;
				bar_size1[i]=0;
			}
			for(i=0;i<result_temp.length;i++)
			{
				temp=result_temp[i].split("\\*");
				if(!temp[0].equals(""))
				{
					for(int j=0;j<bar_name.length;j++)
					{
						tempo=temp[0].split("\\-");
						tmp12=tempo[1]+"/" +tempo[0];
						if(bar_name[j].equals(tmp12)==true)
							bar_size[j]=Integer.parseInt(temp[1]);
					}
				}
			}
			result_temp=results[2].split("\\$");
			for(i=0;i<result_temp.length;i++)
			{
				temp=result_temp[i].split("\\*");
				if(!temp[0].equals(""))
				{
					for(int j=0;j<bar_name.length;j++)
					{
					tempo=temp[0].split("\\-");
					tmp12=tempo[1]+"/" +tempo[0];
					if(bar_name[j].equals(tmp12)==true)
						bar_size1[j]+=Integer.parseInt(temp[1]);
					}
				}
			}
			
			//for(i=0;i<bar_size.length;i++)
				//bar_size[i]=Integer.parseInt(result_temp[i]);
				histo=new HistogramFrame(bar_size.length,10,5,Color.red,Color.blue,bar_size,bar_size1,bar_name,"Numbers","Date","Book Report for BookID: "+st,"purches","search");
				histo.setVisible(true);
		}
			
	
	}
	/**
	 * Update the database about a search that was executed for the books in data array.
	 * @param data - Data about the books which were searched for.
	 */
	public void lsearchUpdate(String[] data)
	{
		//result_temp=data.split("\\:");
		update=new String[3];
		update[2]="";
		for(i=0;i<data.length;i++)
		{
			result_temp=data[i].split("\\:");
			update[2]=update[2]+result_temp[0]+"*";
		}
		update[0]=Main.gusers.id;
		update[1]="searchupdate";
		Main.log.connection.accept(update[1]);
		while(Main.log.flag==false);
		Main.log.flag=false;



	}
	/**
	 * Create searchBookC and searchBookG and make searchBookG visible instead of beg(BookReportGUI).
	 */
	public void lload()
	{
		Main.welcomeScreen.panel.setEnabled(false);
		Main.welcomeScreen.panel.setVisible(false);
		Main.searchBookC = new SearchBookCONTROL();
		Main.searchBookG = new SearchBookGUI();
		Main.searchBookG.flag=2;
		Main.log.frame.setSize(730, 560);
		Main.log.frame.add(Main.searchBookG);
		Main.searchBookG.setEnabled(true);
		Main.searchBookG.setVisible(true);
		Main.log.frame.setVisible(true);
		Main.log.frame.setEnabled(true);
		Main.searchBookG.jLironButton.setVisible(true);
		Main.searchBookG.jLironButton2.setVisible(true);
		Main.searchBookG.AddNewBook.setVisible(false);
		Main.searchBookG.RemoveFC.setVisible(false);
		Main.searchBookG.addTC.setVisible(false);
		Main.searchBookG.UpDate1.setVisible(false);
		beg.setVisible(false);
		beg.setEnabled(false);
	}
}
